package com.twang.stock.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.entity.*;
import com.twang.stock.entity.ex.CompanyStockMinutesEx;
import com.twang.stock.mapper.CompanyStockMinutesMapper;
import com.twang.stock.mapper.LongHuInfoMapper;
import com.twang.stock.mapper.LongHuInfoXiWeiMapper;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.service.*;
import com.twang.stock.service.jinyan.HolidayService;
import com.twang.stock.task.QueryBanKuaiAndDaPan;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * 猫狗榜
 */
@Service
public class LongHuBangServiceImpl implements LongHuBangService {
    @Resource
    private LongHuInfoMapper longHuInfoMapper;

    @Resource
    private CompanyStockMinutesMapper companyStockMinutesMapper;

    @Resource
    private IndexService indexService;

    @Resource
    private ZhangTingBanService zhangTingBanService;

    @Resource
    private HolidayService holidayService;

    @Resource
    private LongHuInfoXiWeiMapper longHuInfoXiWeiMapper;

    @Resource
    private ZhangTingBanTaskService zhangTingBanTaskService;

    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private MinutesChengJIaoeService minutesChengJIaoeService;

    @Override
    public List<LongHuInfo> query(LongHuInfo param) {
        Example example = new Example(LongHuInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotEmpty(param.getStockCode())){
            criteria.andEqualTo("stockCode",param.getStockCode());
        }
        if(StringUtils.isNotEmpty(param.getDate())){
            criteria.andEqualTo("date",param.getDate());
        }else{
            Example example1 = new Example(LongHuInfo.class);
            example1.orderBy("id").desc();
            String date=longHuInfoMapper.selectByExample(example1).get(0).getDate();
            criteria.andEqualTo("date",date);
        }
        example.orderBy("id").desc();
        return longHuInfoMapper.selectByExample(example);
    }

    @Override
    public List<LongHuInfoXiWei> queryLongXiWei(LongHuInfoXiWei param) {
        Example example = new Example(LongHuInfoXiWei.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("longHuId", param.getLongHuId());
        if(StringUtils.isNotEmpty(param.getBuyOrSale())) {
            criteria.andEqualTo("buyOrSale", param.getBuyOrSale());
        }
       // example.orderBy("xiWeiRow").asc();
        return longHuInfoXiWeiMapper.selectByExample(example);
    }

    @Override
    public List<DfcfBanKuaiMaoLiLv> getLongHuDataList(LongHuInfo param) {
        List<DfcfBanKuaiMaoLiLv> maoLiLvs;
        //
        if (StringUtils.isEmpty(param.getDate())) {
            List<String> date = longHuInfoMapper.queryDates();
            param.setDate(date.get(date.size() - 1));
        }
        String key = "GETLONGHUDATALIST_REDIS_" + param.getDate();
        Object obj = redisUtil.get(key);
        if (null == obj||obj.toString().length()<50) {
            List<LongHuInfo> longHuInfoList = this.query(param);
            maoLiLvs = getMaoLiLvDTO(longHuInfoList);
            zhangTingBanTaskService.initKanDian(maoLiLvs, Boolean.FALSE);
            //30 Min 成交额
            zhangTingBanTaskService.getTrade30MinutesVol(maoLiLvs);
            redisUtil.set(key,JSONObject.toJSONString(maoLiLvs));
        } else {
            maoLiLvs = JSONObject.parseArray(obj.toString(), DfcfBanKuaiMaoLiLv.class);
        }

        return maoLiLvs;
    }

    /**
     * 获取看点
     * @param list
     */
    private List<DfcfBanKuaiMaoLiLv> getMaoLiLvDTO(List<LongHuInfo> list) {
        List<DfcfBanKuaiMaoLiLv> maoLiLvs = Lists.newArrayList();
        for (int i = 0; i < list.size(); i++) {
            LongHuInfo longHuInfo = list.get(i);
            /** * 6-今日大盘 */
            try {
                String code = longHuInfo.getStockCode().replace("sz", "").replace("sh", "");
                String stockCode = longHuInfo.getStockCode();
                DfcfBanKuaiMaoLiLv dfcfBanKuaiMaoLiLv = new DfcfBanKuaiMaoLiLv();
                dfcfBanKuaiMaoLiLv.setCode(code);
                dfcfBanKuaiMaoLiLv.setName(longHuInfo.getStockName());
                dfcfBanKuaiMaoLiLv.setMll(longHuInfo.getZdf());
                String queryCode="";
                if ( code.startsWith("6")) {
                    queryCode="sh"+code;
                }else{
                    queryCode="sz"+code;
                }
                CompanyStockMinutes companyStockMinutes= companyStockMinutesMapper.queryByCode(queryCode);
                dfcfBanKuaiMaoLiLv.setZrZhangDie(companyStockMinutes.getZhangDie());
                dfcfBanKuaiMaoLiLv.setLianXuZhang(indexService.isLianXuShangZhang(dfcfBanKuaiMaoLiLv));
                dfcfBanKuaiMaoLiLv.setCje(companyStockMinutes.getTradeSum());
                //List<ThsGeGuReZDian> thsGeGuReZDianList = QueryBanKuaiAndDaPan.查询个股热点(code);
                List<ThsGeGuReZDian> thsGeGuReZDianList=Lists.newArrayList();
                //查询涨停原因
                ThsGeGuReZDian thsGeGuReZDian = new ThsGeGuReZDian();
                thsGeGuReZDian.setTagName("历史涨停原因");
                CompanyStockMinutesEx item=new CompanyStockMinutesEx();
                item.setCompanyCode(code);
                minutesChengJIaoeService.extractedGetZhangTingDesc(item);
                thsGeGuReZDian.setZtDesc(item.getZtDesc());
                thsGeGuReZDianList.add(thsGeGuReZDian);
                dfcfBanKuaiMaoLiLv.setKanDian(thsGeGuReZDianList);
                maoLiLvs.add(dfcfBanKuaiMaoLiLv);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return maoLiLvs;
    }
}
